////////////////////////////////////////////////////////////////////////////////
// Filename: sunsystemclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "sunsystemclass.h"

Planetmodels Sunsystem::pmodels;
Sunsystem::Sunsystem()
{
	Sunsystem::pmodels = Planetmodels();
}
Sunsystem::~Sunsystem()
{
}
bool Sunsystem::initialize(HWND hwnd, D3DClass* m_D3D) {
	int i;
	float red, green, blue;
	bezierstep = 0.0f;
	maxSteps = 600;
	if(!pmodels.initialize(hwnd, m_D3D))
		return false;
	//Initialize Standard-Orbit-Beziercurve
	beziercurve.initialize(7,maxSteps);
	beziercurve.addPoint(D3DXVECTOR3(-1,0,0));
	beziercurve.addPoint(D3DXVECTOR3(-1,1,0));
	beziercurve.addPoint(D3DXVECTOR3(1,1,0));
	beziercurve.addPoint(D3DXVECTOR3(1,0,0));
	beziercurve.addPoint(D3DXVECTOR3(1,-1,0));
	beziercurve.addPoint(D3DXVECTOR3(-1,-1,0));
	beziercurve.addPoint(D3DXVECTOR3(-1,0,0));
	//Rotate
	beziercurve.rotateX(((float)rand() / RAND_MAX)*D3DX_PI);
	beziercurve.rotateY(((float)rand() / RAND_MAX)*D3DX_PI);
	beziercurve.rotateZ(((float)rand() / RAND_MAX)*D3DX_PI);
	timer.Initialize();

	red = (float)rand() / RAND_MAX;
	green = (float)rand() / RAND_MAX;
	blue = (float)rand() / RAND_MAX;

	suninfo.color = D3DXVECTOR4(red, green, blue, 1.0f);
	suninfo.scale = ((float)rand()/RAND_MAX)* 2.0f;
	// Store the number of models.
	m_modelCount = 8;

	// Create a list array of the model information.
	m_ModelInfoList = new ModelInfoType[m_modelCount];
	if(!m_ModelInfoList)
	{
		return false;
	}
	float sumdistance = 3.0f + suninfo.scale;
	// Seed the random generator with the current time.
	srand((unsigned int)&beziercurve);

	// Go through all the models and randomly generate the model color and position.
	for(i=0; i<m_modelCount; i++)
	{
		// Generate a random position
		m_ModelInfoList[i].distanceToSun = 5.0f + sumdistance + (((float)rand()-(float)rand())/RAND_MAX) * 5.0f;
		sumdistance = m_ModelInfoList[i].distanceToSun;
		m_ModelInfoList[i].stepdifference = ((float)rand()/RAND_MAX);
		m_ModelInfoList[i].bezierstep = 0.0f;
		m_ModelInfoList[i].scale = ((float)rand()/RAND_MAX)* 2.0f;
		m_ModelInfoList[i].model = pmodels.getModel(rand() % pmodels.getModelcount());
		m_ModelInfoList[i].rX = ((float)rand()/RAND_MAX)*D3DX_PI/1000.0f;
		m_ModelInfoList[i].rY = ((float)rand()/RAND_MAX)*D3DX_PI/1000.0f;
		m_ModelInfoList[i].rZ = ((float)rand()/RAND_MAX)*D3DX_PI/1000.0f;
		m_ModelInfoList[i].currentRX = 0.0f;
		m_ModelInfoList[i].currentRY = 0.0f;
		m_ModelInfoList[i].currentRZ = 0.0f;
	}
	
	return true;
}
int Sunsystem::getModelCount() {
	return m_modelCount;
}
void Sunsystem::frame() {
	timer.Frame();
	for(int i=0; i<m_modelCount; i++)
	{
		float t = timer.GetTime();
		m_ModelInfoList[i].currentRX += m_ModelInfoList[i].rX*t;
		m_ModelInfoList[i].currentRY += m_ModelInfoList[i].rY*t;
		m_ModelInfoList[i].currentRZ += m_ModelInfoList[i].rZ*t;
		if(m_ModelInfoList[i].currentRX>2.0f*D3DX_PI)
			m_ModelInfoList[i].currentRX = m_ModelInfoList[i].currentRX - 2.0f*D3DX_PI;
		if(m_ModelInfoList[i].currentRY>2.0f*D3DX_PI)
			m_ModelInfoList[i].currentRY = m_ModelInfoList[i].currentRY - 2.0f*D3DX_PI;
		if(m_ModelInfoList[i].currentRZ>2.0f*D3DX_PI)
			m_ModelInfoList[i].currentRZ = m_ModelInfoList[i].currentRZ - 2.0f*D3DX_PI;
		D3DXMatrixRotationYawPitchRoll(&m_ModelInfoList[i].rotation, m_ModelInfoList[i].currentRY, m_ModelInfoList[i].currentRX, m_ModelInfoList[i].currentRZ);
		m_ModelInfoList[i].bezierstep = m_ModelInfoList[i].bezierstep + (t/((float)maxSteps*m_ModelInfoList[i].distanceToSun));
		if(m_ModelInfoList[i].bezierstep > 1)
			m_ModelInfoList[i].bezierstep = 0.0f;
	}
}
D3DXVECTOR4 Sunsystem::getSunColor() {
	return suninfo.color;
}
float Sunsystem::getSunScale() {
	return suninfo.scale;
}
float Sunsystem::getPlanetScale(int planetnumber) {
	return m_ModelInfoList[planetnumber].scale;
}
void Sunsystem::getPlanetposition(float &positionX, float &positionY, float &positionZ, int planetnumber)
{
	float step = m_ModelInfoList[planetnumber].bezierstep + m_ModelInfoList[planetnumber].stepdifference;
	if(step > 1.0f)
		step = step - 1.0f;
	D3DXVECTOR3 pos = beziercurve.getPosition(step);
	pos = pos * m_ModelInfoList[planetnumber].distanceToSun;
	positionX = pos.x;
	positionY = pos.y;
	positionZ = pos.z;
}
ModelClass *Sunsystem::getModel(int planetnumber) {
	return m_ModelInfoList[planetnumber].model;
}
void Sunsystem::getPlanetrotation(int planetnumber, D3DXMATRIX &rotation) {
	rotation = m_ModelInfoList[planetnumber].rotation;
}